﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Http;
using Core;
using Data;
using Model;
using Utils;

namespace Web.Areas.sys.Controllers
{
    public class UserController : BaseController
    {
        //
        // GET: /sys/User/

        public ActionResult Index() {
            var model = new {
                idField = "user_code",
                urls = new {
                    query = "/api/sys/user",
                    add = "/sys/user/edit",
                    edit = "/sys/user/edit/",
                    remove = "/api/sys/user/edit/"
                },
                resx = new {
                    detailTitle = "Detail",
                    editTitle = "Edit User",
                    addTitle = "Add User",
                    removeComfirm = "Are you sure you want to delete this record?",
                    removeSuccess = "delete successfully!",
                    removeFailed = "delete failed!"
                },
                dataSource = new {
                    items = new sys_userService().GetModels()
                },
                form = new {
                    user_code = "",
                    user_name = "",
                    remark = ""
                }
            };
            return View(model);
        }


        public ActionResult Edit(string id) {
            var data = new UserApiController().GetMaster(id);

            var model = new {
                form = data.form,
                urls = new {
                    edit = "/api/sys/user/edit"
                },
                resx = new {
                    saveSuccess = "Save Successfully!",
                    editSuccess = "Edit Successfully!"
                },
                dataSource = new { test = "test" },
                defaultForm = new sys_user() {
                    user_code = "",
                    state = 0
                },
                setting = new {
                    disabledFieldsInEdit = new string[] { "password" }
                }

            };

            return View(model);
        }

    }

    public class UserApiController : ApiController
    {
        private string _xml = "User.xml";
        sys_userService _service = new sys_userService();

        public dynamic Get(ReqWrapperQuery req) {
            var pq = req.ToParam(_xml);
            var ret = _service.GetDynamicListWithPaging(pq);
            return ret;
        }


        public dynamic GetMaster(string id) {
            var model = _service.GetModel(ParamQuery.Instance().AddWhere("user_code", id));
            return new {
                form = model
            };
        }

        [System.Web.Http.HttpPost]
        public int Edit(ReqWrapperInsert req) {
            var pi = req.ToParam(_xml);
            var pwd = pi.FieldInserts.FirstOrDefault(x => x.Name == "password");
            pwd.Value = Md5Hash.GetMd5Hash(pwd.Value);
            return _service.Insert(pi);
        }

        [System.Web.Http.HttpPut]
        public int Edit(ReqWrapperUpdate req) {
            var pi = req.ToParam(_xml);
            return _service.Update(pi);
        }

        [System.Web.Http.HttpDelete]
        public int Edit(string id) {
            return new sys_userService().Update(new ParamBase().AddFieldUpdate("state", "2").AddWhere("user_code", id));
        }
    }
}
